/***************************************************************************
 *   Copyright (C) 2007 by Ziman & Xloverin                                *
 *   ziman@centrum.sk, xloverin@gmail.com                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef LOG_H
#define LOG_H

#include <QObject>
#include <QMap>
#include <QFile>
#include <QDir>
#include <QDateTime>
#include "prefslog.h"

class Terminal;
/**
 * Tato trieda sa stara o logovanie.
 * @author Ziman & Xloverin <ziman@centrum.sk, xloverin@gmail.com>
 */
class Log : public QObject {

	/**
	 * Sfiltruje nazov query takto:
	 * 1. Nahradi vsetky nelfanumericke znaky podciarovnikom.
	 * 2. Nahradi vsetky sekvencie podciarovnikov jednym podciarovnikom.
	 */
	static QString filter(QString title);

	/**
	 * Vrati vzdy aspon dvojciferne cislo. Ak je x < 10, doplni ho nulou.
	 */
	static QString two(int x);

	Q_OBJECT
public:

	/// Nastavenia.
	typedef PrefsLog::Prefs Prefs;

	/**
	 * Sformatuje timestamp alebo nazov suboru podla zadaneho formatu.
	 * Specialne znaky:
	 * %q -- Filtrovany nazov query (tj. title) (@see Log#filter)
	 * %y -- Rok
	 * %m -- Mesiac
	 * %d -- Den
	 * %H -- Hodina
	 * %M -- Minuta
	 * %S -- Sekunda
	 * %% -- %
	 * @param format Format nazvu.
	 * @param title Nazov query.
	 * @return Sformatovany timestamp alebo nazov suboru.
	 */
	static QString format(QString format, QString title, QDateTime now = QDateTime::currentDateTime());

	/// Nastavi logovanie.
	void setPrefs(const Prefs &prefs);

	/// Zastavi logovanie.
	void stop();

	/**
	 * Zapise jeden zaznam do suboru. Ak bolo logovanie zastavene,
	 * automaticky pootvara potrebne subory.
	 */
	void write(QString title, PrefsLog::Format format, QString text, Terminal *terminal);

    Log();
    virtual ~Log();

private:

	/// Nastavenia.
	Prefs prefs;

	/// Mapa nazov suboru -> prislusny objekt.
	QMap<QString, QFile *> files;

	/**
	 * Vrati QFile objekt asociovany so suborom so zadanym menom.
	 * Ak este neexistuje, vytvori ho a otvori na zapis na koniec.
	 */
	QFile *getFile(const QString &fileName, PrefsLog::Format format, Terminal *terminal);
};

#endif
